//
//  LGClinicMedicalRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/12.
//

#import "LGClinicMedicalRecordController.h"
#import "LGGetWebViewController.h"
#import "LGReportDetailHeaderView.h"
#import "LGOutpatientRecordDetailCell.h"
#import "LGReportDetailFileCell.h"
@interface LGClinicMedicalRecordController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)LGReportDetailHeaderView *headerView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
/**   */
@property (nonatomic , strong)NSString *filePath;

@end

@implementation LGClinicMedicalRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"报告详情";
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Visitrecords/visit1Detail" parameters:@{@"record_id":self.recordId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataDic = returnData;
            self.headerView.dataDic = returnData;
            self.filePath = LGNSString(returnData[@"report_files"]);
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)checkFileInformation:(UIButton *)sender{
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.navigationView.title = self.dataDic[@"report_name"];
    controller.urlStr = self.filePath;
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 1;
    }else{
        return self.filePath.length>0?1:0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        LGOutpatientRecordDetailCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"contentCell" forIndexPath:indexPath];
        contentCell.selectionStyle = UITableViewCellSelectionStyleNone;
        contentCell.dataDic = self.dataDic;
        return contentCell;
        
    }else{
        LGReportDetailFileCell *fileCell = [tableView dequeueReusableCellWithIdentifier:@"fileCell" forIndexPath:indexPath];
        fileCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fileCell.title = [NSString stringWithFormat:@"%@.pdf",self.dataDic[@"report_name"]];
        [fileCell.checkBtn addTarget:self action:@selector(checkFileInformation:) forControlEvents:UIControlEventTouchUpInside];
        return fileCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return (section==0)?0.1:viewPix(50);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(section == 0){
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.1)];
        return headView;
    }else{
        LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
        headView.topMargin = viewPix(8);
        UIButton *titleBtn = [UIButton buttonWithTitle:@"报告原文件" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#1F6CC6" imageName:@""];
        titleBtn.frame = CGRectMake(viewPix(16), viewPix(20), 112, 27);
        [titleBtn setBackgroundImage:[UIImage imageNamed:@"就诊检查-蓝色"] forState:UIControlStateNormal];
        [headView addSubview:titleBtn];
        return headView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return (section==0)?0.1:viewPix(16);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section == 0){
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.1)];
        return footView;
        
    }else{
        LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(16))];
        footView.bottomMargin = viewPix(8);
        return footView;
    }
}

-(LGReportDetailHeaderView *)headerView{
    if(!_headerView){
        _headerView = [[LGReportDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(130))];
    }
    return _headerView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGOutpatientRecordDetailCell class] forCellReuseIdentifier:@"contentCell"];
        [_tableView registerClass:[LGReportDetailFileCell class] forCellReuseIdentifier:@"fileCell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
